package com.leyou.auth.config;

import com.leyou.auth.interceptors.LoginInterceptor;
import com.leyou.auth.utils.JwtUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Collection;

/**
 * @author BaiZY on 2020/9/4.
 * @version 1.0
 */
public class MvcConfiguration implements WebMvcConfigurer {

    private final JwtUtils jwtUtils;
    private final ClientProperties clientProperies;

    public MvcConfiguration(@Lazy JwtUtils jwtUtils, ClientProperties clientProperies) {
        this.jwtUtils = jwtUtils;
        this.clientProperies = clientProperies;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册拦截器,并得到拦截器注册器
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor(jwtUtils));

        //判断用户是否配置了拦截路径,如果配置没走默认,就是拦截/**
        if (!CollectionUtils.isEmpty(clientProperies.getIncludeFilterPaths())){
            registration.addPathPatterns(clientProperies.getIncludeFilterPaths());
        }
        if (!CollectionUtils.isEmpty(clientProperies.getExcludeFilterPaths())){
            registration.excludePathPatterns(clientProperies.getExcludeFilterPaths());
        }
    }
}
